package com.example.crm01.controller.system;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.crm01.common.Result;
import com.example.crm01.entity.system.Equipment;
import com.example.crm01.entity.system.Region;
import com.example.crm01.entity.system.Role;
import com.example.crm01.entity.system.ServicePackage;
import com.example.crm01.entity.system.SystemBusiness;
import com.example.crm01.page.TableDataInfo;
import com.example.crm01.service.EquipmentService;
import com.example.crm01.service.RegionService;
import com.example.crm01.service.RoleService;
import com.example.crm01.service.ServicePackageService;
import com.example.crm01.service.SystemBusinessService;
import com.example.crm01.service.form.BaseIdForm;
import com.example.crm01.service.form.system.EquipmentAddForm;
import com.example.crm01.service.form.system.EquipmentListForm;
import com.example.crm01.service.form.system.RegionAddForm;
import com.example.crm01.service.form.system.RegionListForm;
import com.example.crm01.service.form.system.RoleAddForm;
import com.example.crm01.service.form.system.RoleListForm;
import com.example.crm01.service.form.system.ServicePackageAddForm;
import com.example.crm01.service.form.system.ServicePackageListForm;
import com.example.crm01.service.form.system.SystemBusinessAddForm;
import com.example.crm01.service.form.system.SystemBusinessListForm;
import com.example.crm01.service.vo.system.ServicePackageDTO;
import com.example.crm01.service.vo.system.SystemBusinessVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;


@Api(tags = "设置")
@RestController
@RequestMapping("/system")
@Slf4j
public class SystemController {

    public final RegionService regionService;
    public final EquipmentService equipmentService;
    public final RoleService roleService;
    public final SystemBusinessService systemBusinessService;
    public final ServicePackageService servicePackageService;

    public SystemController(RegionService regionService, EquipmentService equipmentService, RoleService roleService, SystemBusinessService systemBusinessService, ServicePackageService servicePackageService) {
        this.regionService = regionService;
        this.equipmentService = equipmentService;
        this.roleService = roleService;
        this.systemBusinessService = systemBusinessService;
        this.servicePackageService = servicePackageService;
    }

    /*-----------------设备管理-----------------------------*/
    @ApiOperation("设备列表")
    @PostMapping(value = "/equipment/list")
    public TableDataInfo listEquipment(@RequestBody EquipmentListForm form) {
        TableDataInfo info = new TableDataInfo();
        log.info("打印入参的参数，form：{}", form);
        Page<Equipment> systemBusinessList = equipmentService.listEquipment(form);
        info.setCode(200);
        info.setMsg("请求成功");
        info.setRows(systemBusinessList.getRecords());
        info.setTotal(systemBusinessList.getTotal());
        return info;
    }

    @ApiOperation("设备新增")
    @PostMapping(value = "/equipment/add")
    public Result<Boolean> addEquipment(@RequestBody @Validated EquipmentAddForm form) {
        return Result.success(equipmentService.addEquipment(form));
    }

    @ApiOperation("设备修改")
    @PostMapping(value = "/equipment/update")
    public Result<Boolean> updateEquipment(@RequestBody @Validated EquipmentAddForm form) {
        return Result.success(equipmentService.updateEquipment(form));
    }

    @ApiOperation("设备删除")
    @PostMapping(value = "/equipment/delete")
    public Result<Boolean> deleteEquipment(@RequestBody @Validated BaseIdForm form) {
        return Result.success(equipmentService.deleteEquipment(form));
    }

    /*-----------------区域管理-----------------------------*/
    @ApiOperation("区域列表")
    @PostMapping(value = "/region/list")
    public TableDataInfo listRegion(@RequestBody RegionListForm form) {
        TableDataInfo info = new TableDataInfo();
        log.info("打印入参的参数，form：{}", form);
        Page<Region> systemBusinessList = regionService.regionList(form);
        info.setCode(200);
        info.setMsg("请求成功");
        info.setRows(systemBusinessList.getRecords());
        info.setTotal(systemBusinessList.getTotal());
        return info;
    }

    @ApiOperation("新增区域")
    @PostMapping(value = "/region/add")
    public Result<Boolean> addRegion(@RequestBody @Validated RegionAddForm form) {
        return Result.success(regionService.addRegion(form));
    }

    @ApiOperation("修改区域")
    @PostMapping(value = "/region/update")
    public Result<Boolean> updateRegion(@RequestBody @Validated RegionAddForm form) {
        return Result.success(regionService.updateRegion(form));
    }

    @ApiOperation("删除区域")
    @PostMapping(value = "/region/delete")
    public Result<Boolean> deleteRegion(@RequestBody @Validated BaseIdForm form) {
        return Result.success(regionService.deleteRegion(form));
    }
    /*-----------------角色管理-----------------------------*/
    @ApiOperation("角色列表")
    @PostMapping(value = "/role/list")
    public TableDataInfo listRole(@RequestBody RoleListForm form) {
        TableDataInfo info = new TableDataInfo();
        log.info("打印入参的参数，form：{}", form);
        Page<Role> systemBusinessList = roleService.listRole(form);
        info.setCode(200);
        info.setMsg("请求成功");
        info.setRows(systemBusinessList.getRecords());
        info.setTotal(systemBusinessList.getTotal());
        return info;
    }

    @ApiOperation("角色新增")
    @PostMapping(value = "/role/add")
    public Result<Boolean> addRole(@RequestBody @Validated RoleAddForm form) {
        return Result.success(roleService.addRole(form));
    }

    @ApiOperation("角色修改")
    @PostMapping(value = "/role/update")
    public Result<Boolean> updateRole(@RequestBody @Validated RoleAddForm form) {
        return Result.success(roleService.updateRole(form));
    }

    @ApiOperation("角色删除")
    @PostMapping(value = "/role/delete")
    public Result<Boolean> deleteRole(@RequestBody @Validated BaseIdForm form) {
        return Result.success(roleService.deleteRole(form));
    }

    /*-----------------基础业务信息管理-----------------------------*/
    @ApiOperation("基础业务信息列表")
    @PostMapping(value = "/systemBusiness/list")
    public TableDataInfo listSystemBusiness(@RequestBody SystemBusinessListForm form) {
        TableDataInfo info = new TableDataInfo();
        log.info("打印入参的参数，form：{}", form);
        Page<SystemBusiness> systemBusinessList = systemBusinessService.listSystemBusiness(form);
        info.setCode(200);
        info.setMsg("请求成功");
        List<SystemBusiness> records = systemBusinessList.getRecords();
        if (!CollectionUtils.isEmpty(records)){
            List<SystemBusinessVO> collect = records.stream().map(entity -> {
                SystemBusinessVO systemBusinessVO = new SystemBusinessVO();
                systemBusinessVO.setId(entity.getId());
                systemBusinessVO.setName(entity.getName());
                systemBusinessVO.setRegionId(entity.getRegionId());
                systemBusinessVO.setRegionName(entity.getRegionName());
                //解析json格式的数据,封装成dto
                List<ServicePackageDTO> servicePackageDTOS = JSON.parseArray(entity.getServicePackage(), ServicePackageDTO.class);
                systemBusinessVO.setDtoList(servicePackageDTOS);
                //计算总的价格
                BigDecimal totalPrice = servicePackageDTOS.stream()
                        .map(ServicePackageDTO::getPrice)
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                systemBusinessVO.setTotalPrice(totalPrice);
                return systemBusinessVO;
            }).collect(Collectors.toList());
            info.setRows(collect);
        }else {
            info.setRows(systemBusinessList.getRecords());
        }
        info.setTotal(systemBusinessList.getTotal());
        return info;
    }

    @ApiOperation("基础业务信息新增")
    @PostMapping(value = "/systemBusiness/add")
    public Result<Boolean> addSystemBusiness(@RequestBody @Validated SystemBusinessAddForm form) {
        return Result.success(systemBusinessService.addSystemBusiness(form));
    }

    @ApiOperation("基础业务信息修改")
    @PostMapping(value = "/systemBusiness/update")
    public Result<Boolean> updateSystemBusiness(@RequestBody @Validated SystemBusinessAddForm form) {
        return Result.success(systemBusinessService.updateSystemBusiness(form));
    }

    @ApiOperation("基础业务信息删除")
    @PostMapping(value = "/systemBusiness/delete")
    public Result<Boolean> deleteSystemBusiness(@RequestBody @Validated BaseIdForm form) {
        return Result.success(systemBusinessService.deleteSystemBusiness(form));
    }

    /*-----------------套餐管理-----------------------------*/
    @ApiOperation("套餐列表")
    @PostMapping(value = "/service/package/list")
    public TableDataInfo listServicePackage(@RequestBody ServicePackageListForm form) {
        TableDataInfo info = new TableDataInfo();
        log.info("打印入参的参数，form：{}", form);
        Page<ServicePackage> systemBusinessList = servicePackageService.listSystemBusiness(form);
        info.setCode(200);
        info.setMsg("请求成功");
        info.setRows(systemBusinessList.getRecords());
        info.setTotal(systemBusinessList.getTotal());
        return info;
    }

    @ApiOperation("套餐新增")
    @PostMapping(value = "/service/package/add")
    public Result<Boolean> addServicePackage(@RequestBody @Validated ServicePackageAddForm form) {
        return Result.success(servicePackageService.addServicePackage(form));
    }

    @ApiOperation("套餐修改")
    @PostMapping(value = "/service/package/update")
    public Result<Boolean> updateServicePackage(@RequestBody @Validated ServicePackageAddForm form) {
        return Result.success(servicePackageService.updateServicePackage(form));
    }

    @ApiOperation("套餐删除")
    @PostMapping(value = "/service/package/delete")
    public Result<Boolean> deleteServicePackage(@RequestBody @Validated BaseIdForm form) {
        return Result.success(servicePackageService.deleteServicePackage(form));
    }
}
